package com.stone.back.order.dao;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.stone.back.order.entity.BfOrder;
import com.stone.back.order.entity.BfOrderInfo;
import com.stone.base.dao.BaseDao;
import com.stone.base.entity.Page;
import java.util.List;

/**
 * 订单管理
 * @author Stone P
 *
 */
@Repository
public class OrderDao extends BaseDao implements IOrderDao {
	/**
	 * 查询订单分页信息
	 * @param pageNumber			页数
	 * @param priceMin				订单总价
	 * @param priceMax				订单总价
	 * @param startCreateDate		订餐时间
	 * @param endCreateDate			订餐时间
	 * @param status				订单状态
	 * @return
	 */
	@Override
	public Page selectOrderPage(int pageNumber, String priceMin, String priceMax, String startCreateDate, String endCreateDate, String status) {
		StringBuilder hql = new StringBuilder("from BfOrder where 1=1 ");
		if(StringUtils.isNotBlank(priceMax)) {
			hql.append(" and totalprices <= " + priceMax);
		}
		if(StringUtils.isNotBlank(priceMin)) {
			hql.append(" and totalprices >= " + priceMin);
		}
		if(StringUtils.isNotBlank(startCreateDate)) {
			hql.append(" and createdate >= '" + startCreateDate + "'");
		}
		if(StringUtils.isNotBlank(endCreateDate)) {
			hql.append(" and createdate <= '" + endCreateDate + "'");
		}
		if(StringUtils.isNotBlank(status)) {
			hql.append(" and status = " + status);
		}
		hql.append(" order by orderid desc");
 		Page page = this.queryPage(hql.toString(), pageNumber);
		return page;
	}
	
	/**
	 * 查询订单详情分页信息
	 * @param pageNumber	页数
	 * @param orderId		订单Id
	 * @param name			名称
	 * @param datatype		类型
	 * @param priceMax		单价
	 * @param priceMin		单价	
	 * @return
	 */
	@Override
	public Page selectOrderInfoPage(int pageNumber, String orderId, String datatype, String name, String priceMin, String priceMax) {
		//菜品列表
		StringBuilder sql = new StringBuilder("select * from bfv_order_info where orderid = '" + orderId + "' ");
		if(StringUtils.isNotBlank(name)) {
			sql.append(" and name like '%" + name + "%'");
		}
		if(StringUtils.isNotBlank(priceMax)) {
			sql.append(" and price <= " + priceMax);
		}
		if(StringUtils.isNotBlank(priceMin)) {
			sql.append(" and price >= " + priceMin);
		}
		if(StringUtils.isNotBlank(datatype)) {
			sql.append(" and datatype = " + datatype);
		}
		sql.append(" order by price desc");
 		Page page = this.queryPageBySql(sql.toString(), pageNumber);
		return page;
	}

	/**
	 * 查询订单详情
	 * @param orderId	订单Id
	 * @return
	 */
	@Override
	public BfOrder selectOrderDetail(String orderId) {
		BfOrder order = this.hibernateTemplate.get(BfOrder.class, orderId);
		return order;
	}
        
         /**
         * 查询用户订单
         * @param pageNumber
         * @param userid
         * @return 
         */
        public Page selectOrderByUserId(int pageNumber, Integer userid) {
            String hql = "from BfOrder where userid = " + userid;
            return this.queryPage(hql, pageNumber);
        }

        /**
         * 查询订单项
         * @param oid
         * @return 
         */
        public List<BfOrderInfo> selectOrderInfoByOrderId(String oid) {
            String hql = "from BfOrderInfo where orderid = ?";
            return this.hibernateTemplate.find(hql, oid);
        }
        
	/**
	 * 修改订单信息
	 * @param order	订单信息
	 * @return
	 */
	@Override
	public void updateOrder(BfOrder order) {
		this.hibernateTemplate.update(order);
	}
}
